package com.asdhawaii.otello.util;

import com.asdhawaii.otello.board.Square;
import com.asdhawaii.otello.board.SquareContents;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;

/**
 * Runs various code, mostly to test smaller parts of the Othello game.
 * 
 * @author Branden Ogata
 *
 */

public class Playground
{
  private static int turns = 0;
  
  /**
   * Runs test code.
   * 
   * @param args The String[] containing any command line arguments.
   * 
   */
  
  public static void main(String[] args)
  {
    Square[][] squares = new Square[8][8];
    ButtonListener listener = new ButtonListener();
      
    // Creates a Square and displays it on the screen
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(new GridLayout(8, 8));

    for (int i = 0; i < 8; i++)
    {
      for (int j = 0; j < 8; j++)
      {
        // Center squares
        if (((i == 3) && (j == 3)) || 
            ((i == 4) && (j == 4)))
        {
          squares[i][j] = new Square(SquareContents.BLACK);
          System.out.print("BLACK");
        }
        else if (((i == 3) && (j == 4)) || 
                 ((i == 4) && (j == 3)))
        {
          squares[i][j] = new Square(SquareContents.WHITE);
          System.out.print("WHITE");
        }
        else
        {
          squares[i][j] = new Square(SquareContents.EMPTY);  
          System.out.print("EMPTY");
        }

        System.out.println("(" + j + "," + i + ")");        
        squares[i][j].addActionListener(listener);

        frame.getContentPane().add(squares[i][j]);
      }
    }        

/*
    ImageIcon test = ImageLoader.getImageIcon(new TestBed(), "white-black_00.jpg");
    JLabel image = new JLabel(test);
    JLabel secondImage = new JLabel(test);
    constraints.gridx = 0;
    constraints.gridy = 8;
    frame.add(image);
    constraints.gridx = 1;
    constraints.gridy = 8;
    frame.add(secondImage);
*/
    frame.setVisible(true);
    frame.pack();   
  }
  
  /**
   * Handles button presses.
   * 
   */
  
  private static class ButtonListener implements ActionListener
  {
    /**
     * Handles any button presses that do occur.
     * 
     * @param event An ActionEvent that recorded the button press.
     * 
     */
    
    @Override
    public void actionPerformed(ActionEvent event)
    {
      if (event.getSource() instanceof Square)
      {
        Square square = (Square) event.getSource();

        if (square.getContents().equals(SquareContents.EMPTY))
        {
          square.setContents((turns % 2 == 0) ? (SquareContents.BLACK) : (SquareContents.WHITE));
        }
        else
        {
          square.setContents(square.getContents().opposite());
        }
        
        turns++;
      }
    }    
  }
}
